<template>
	<!-- 个人资料编辑 -->
	<view>
		<view class="avatarBox flex-bc" @click="fnUploadAvatar()">
			<text>头像</text>
			<view class="flex-c">
				<image :src="avatar"></image>
				<uni-icons type="arrowright" size="14" color="#9698A9"></uni-icons>
			</view>
		</view>
		<view class="nickname flex-bc">
			<text>昵称</text>
			<view class="flex-c">
				<input type="text" v-model="nickname" @blur="handleInput()"/>
				<uni-icons type="arrowright" size="14" color="#9698A9"></uni-icons>
			</view>
		</view>
	</view>
</template>

<script>
	import config from '../../../common/config.js'
	export default {
		data() {
			return {
				nickname:'未设置',
				avatar:'',
				token:uni.getStorageSync('token'),
				userInfo:uni.getStorageSync('userInfo')
			};
		},
		onLoad(option) {
			this.nickname = option.nickname
			this.avatar = option.avatar
		},
		methods:{
			handleInput(){
				console.log(this.nickname)
				if(this.nickname){
					this.request({
						url: '/czb/user/updateUserNickname',
						method: 'POST',
						data: {
							'store_id': 1,
							'token':this.token,
							'nickname':this.nickname
						},
					}).then((ret) => {
						console.log(ret);
						if (ret.code == 0) { 
							this.userInfo.nickname = this.nickname 
							this.$Store.commit('setUserInfo', this.userInfo)
							uni.showToast({
								title:ret.message,
								icon:'success',
							})
							setTimeout(function(){
								uni.navigateBack()
							},1500)
						}
					})
				}
			},
			fnUploadAvatar(){
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
				    success: (chooseImageRes) => {
				        this.avatar = chooseImageRes.tempFilePaths[0];
						uni.uploadFile({
							url: `${config.baseUrl}/czb/user/updateUserAvatar`,
							filePath: this.avatar,
							name: 'avatar',
							formData: {
								'store_id': 1,
								'token': uni.getStorageSync('token'),
							},
							success: (uploadFileRes) => {
								var data = JSON.parse(uploadFileRes.data)
								if(data.code == 0){
									this.userInfo.avatar = data.avatar
									this.$Store.commit('setUserInfo', this.userInfo)
									uni.showToast({
										title:data.message,
										icon:'success',
									})
									setTimeout(function(){
										uni.navigateBack()
									},1500)
								}
							},
						});
				    }
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.avatarBox{
		font-size: 30rpx;
		background: #fff;
		padding: 20rpx;
		border-bottom: 1rpx solid #e3e3e3;
		image{
			width: 100rpx;
			height: 100rpx;
			border-radius: 50%;
		}
	}
	.nickname{
		font-size: 30rpx;
		background: #fff;
		padding: 30rpx 20rpx;
		border-bottom: 1rpx solid #e3e3e3;
		input{
			text-align: right;
			font-size: 26rpx;
			color: #666;
		}
	}
	.uni-icons{
		margin-left: 10rpx;
	}
</style>
